// ...........................................................................................................
// (C) Copyright  1996/2008 Fuego Inc.  All Rights Reserved
// THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Fuego Inc.
// The copyright notice above does not evidence any actual or intended
// publication of such source code.
//
// Last changed on 2008-01-31 14:19:06 (-0300), by: pcolunga. $Revision$
// ...........................................................................................................

package com.webcamtracker.tracker.example.spatial;

import com.sun.j3d.utils.geometry.Cylinder;

import javax.media.j3d.Appearance;
import javax.media.j3d.Material;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3f;
import java.awt.*;

public class Stick
        extends TransformGroup {
    //~ Instance fields ......................................................................................

    private final float depth;
    private final float lenght = 16F;

    //~ Constructors .........................................................................................

    public Stick(float x, float y, float depth) {
        setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

        this.depth = depth * 4;

        setInitialPosition(x, y);

        Cylinder cylinder = new Cylinder(0.01F, this.lenght);

        /*Material m = new Material();
//        m.setAmbientColor(new Color3f(Color.GREEN));
//        m.setEmissiveColor(new Color3f(Color.GREEN));
        m.setLightingEnable(true);
        m.setSpecularColor(new Color3f(Color.GREEN));
        m.setShininess(1.0F);
        Appearance appearance = new Appearance();
        appearance.setMaterial(m);

        cylinder.setAppearance(appearance);*/

        /*Appearance a = new Appearance();
        Material mat = new Material();
        mat.setLightingEnable(true);
        mat.setAmbientColor(0.5f, 1.0f, 1.0f);
        a.setMaterial(mat);
        a.setColoringAttributes(
                new ColoringAttributes(1.0f, 0.5f, 0.5f,
                        ColoringAttributes.NICEST));

        cylinder.setAppearance(a);*/

        addChild(cylinder);

        StickTop stickTop = new StickTop();
        addChild(stickTop);
    }

    //~ Methods ..............................................................................................

    private void setInitialPosition(float x, float y) {
        Transform3D position = new Transform3D();

        Vector3f translacion = new Vector3f(x, y, -this.depth);
        position.setTranslation(translacion);

        Transform3D rotateX = new Transform3D();
        rotateX.rotX(Math.PI / 2);

        position.mul(rotateX);

        setTransform(position);
    }

    //~ Inner Classes ........................................................................................

    private class StickTop
            extends TransformGroup {
        public StickTop() {
            setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
            setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

            final Color3f white = new Color3f(Color.WHITE);
            final Color3f red = new Color3f(Color.RED);

            addChild(buildTopCylinder(0.25F, 0.0100F, red));
            addChild(buildTopCylinder(0.20F, 0.0120F, white));
            addChild(buildTopCylinder(0.13F, 0.0160F, red));
            addChild(buildTopCylinder(0.04F, 0.0180F, white));

            setInitialPosition();
        }

        private Cylinder buildTopCylinder(float radius, float z, Color3f color) {
            Cylinder topCylinder = new Cylinder(radius * depth / 6, z, 1, 90, 5, new Appearance());

            Material m = new Material();
            m.setDiffuseColor(color);
            Appearance appearance = new Appearance();
            appearance.setMaterial(m);

            topCylinder.setAppearance(appearance);
            return topCylinder;
        }

        private void setInitialPosition() {
            Transform3D position = new Transform3D();

            Vector3f translacion = new Vector3f(.0F, lenght / 2, .0F);
            position.setTranslation(translacion);

            setTransform(position);
        }
    }
}
